package com.yaoben.exam.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yaoben.exam.pojo.Role;
import com.yaoben.exam.pojo.Team;
import com.yaoben.exam.pojo.User;
import com.yaoben.exam.service.RoleService;
import com.yaoben.exam.service.TeamService;
import com.yaoben.exam.service.UserService;

@Controller
public class UserController {
	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private TeamService teamService;
	
	@RequestMapping("resetPassword")
	public void resetPassword(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String id = request.getParameter("id");
		String pwd = request.getParameter("oldPasswork");
		String newPwd = request.getParameter("newPasswork");
		String surePwd = request.getParameter("surePasswork");
		if(!newPwd.equals(surePwd)){
			System.out.println("different");
			response.getWriter().println("the twice password is different");
			return;
		}
		if(userService.getUserById(Integer.parseInt(id)).getPwd().equals(pwd)){
			User user = new User();
			user.setId(Integer.parseInt(id));
			user.setPwd(newPwd);
			int is = userService.updateUserById(user);
			if(is > 0){
				System.out.println("success");
				response.getWriter().println("your password change successful");
			}
		}else{
			System.out.println("fail");
			response.getWriter().println("Oh my god!fail");
		}
	}
	
	@RequestMapping("test")
	public void test(Integer uid){
		User user=userService.getUserById(uid);
		System.out.println(user);
	}
	
	@RequestMapping("login")
	public String login(){
		
		return "login";
	}
	
	@RequestMapping("usercheck")
	public String userCheck(HttpServletRequest request,HttpServletResponse response){
		String page="error";
		String loginName = request.getParameter("loginname");
		String pwd = request.getParameter("pwd");
		User user = userService.getUserByLoginName(loginName);
		System.out.println(user);
		if(user != null){
			if(user.getPwd().equals(pwd)){
				request.getSession().setAttribute("user", user);
				int roleId = user.getRoleid();
				Role role=roleService.getRoleById(roleId);
				request.getSession().setAttribute("role", role);
				
				int groupId = user.getGroupid();
				System.out.println(groupId);
				Team team = teamService.getTeamById(groupId);

				request.getSession().setAttribute("team", team);
				
				switch(roleId){
				//普通用户
				case 1:
					page="user/Frame_main";
					request.getSession().setAttribute("user", user);
					break;
				//队长
				case 2:
					page="test";
					break;
				//面试官
				case 3:
					page="interview/Inter_Frame_main";
					break;
				//临时用户
				case 4:
					page="test";
					break;
				//讲师
				case 5:
					page="test";
					break;
				//管理员
				case 6:
					page="administration";
					break;
				default:
					page="test";
					break;
				}
			}else{
				page="login";
			}
		}else{
			//System.out.println(121);
			page="error";
		}
		return page;
	}

	@RequestMapping("Frame_top")
	public String userTop(){
		return "user/Frame_top";
	}
	
	@RequestMapping("Frame_left")
	public String userLeft(){
		return "user/Frame_left";
	}
	@RequestMapping("passwork_revise")
	public String userRevice(){
		return "user/passwork_revise";
	}
	@RequestMapping("user_information")
	public String userinformation(){
		return "user/user_information";
	}
	@RequestMapping("Frame_right_blank")
	public String userBlank(){
		return "user/Frame_right_blank";
	}
	
	@RequestMapping("score_check")
	public String score_check(){
		return "exam/score_check";
	}
	@RequestMapping("exam_enter")
	public String exam_enter(){
		return "exam/exam_enter";
	}
	
	@RequestMapping("exam_now")
	public String exam_now(){
		return "exam/exam_now";
	}
	
	@RequestMapping("exercise_upload")
	public String exercise_upload(){
		return "exam/exercise_upload";
	}
	@RequestMapping("Inter_Frame_left")
	public String inter_Frame_left(){
		return "interview/Inter_Frame_left";
	}
	
	@RequestMapping("interim_information")
	public String interim_information(){
		return "interview/interim_information";
	}
}
